package view;

import java.awt.Color;
import model.Painel;
import javax.swing.JOptionPane;

/**
 *
 * @author Diogo
 */
public class MainMenu extends javax.swing.JFrame {

    boolean[] selection;
    /**
     * Creates new form MainMenu
     */
    public MainMenu() {
        initComponents();
        selection = new boolean[4];
        setResizable(false);
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    desenharPista();
                    try {
                        sleep(500);
                    } catch (InterruptedException ex) {
                        JOptionPane.showMessageDialog(null, "Thread parada", "", 1);
                    }
                }
            }
        }.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new Painel();
        jComboBox2 = new javax.swing.JComboBox();
        jButtonDefPista = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButtonSolRec = new javax.swing.JButton();
        jCheckBox4 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jButtonLibRec = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(51, 51, 51));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 388, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pista 1", "Pista 2", "Pista 3", "Pista 4" }));

        jButtonDefPista.setText("Def. Pista Atual");
        jButtonDefPista.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDefPistaActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonSolRec.setText("Solicitar Recurso");
        jButtonSolRec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSolRecActionPerformed(evt);
            }
        });

        jCheckBox4.setText("Recurso 4");

        jCheckBox3.setText("Recurso 3");

        jCheckBox2.setText("Recurso 2");

        jCheckBox1.setText("Recurso 1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox1)
                    .addComponent(jCheckBox2)
                    .addComponent(jCheckBox3)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jCheckBox4)
                        .addGap(6, 6, 6)
                        .addComponent(jButtonSolRec, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox4)
                .addContainerGap(23, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonSolRec)
                .addContainerGap())
        );

        jButtonLibRec.setText("Liberar Recursos");
        jButtonLibRec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLibRecActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonDefPista, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonLibRec, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButtonDefPista))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 110, Short.MAX_VALUE)
                        .addComponent(jButtonLibRec))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void desenharPista() {
        ((Painel) jPanel1).desenharLinha(0, 100, 150, 100);
        ((Painel) jPanel1).desenharLinha(0, 200, 150, 200);
        ((Painel) jPanel1).desenharLinha(150, 0, 150, 100);
        ((Painel) jPanel1).desenharLinha(250, 0, 250, 100);
        ((Painel) jPanel1).desenharLinha(250, 100, 400, 100);
        ((Painel) jPanel1).desenharLinha(250, 200, 400, 200);
        ((Painel) jPanel1).desenharLinha(250, 200, 250, 300);
        ((Painel) jPanel1).desenharLinha(150, 200, 150, 300);
        ((Painel) jPanel1).desenharFaixa(0, 150, 150, 150);
        ((Painel) jPanel1).desenharFaixa(200, 100, 200, 0);
        ((Painel) jPanel1).desenharFaixa(250, 150, 400, 150);
        ((Painel) jPanel1).desenharFaixa(200, 200, 200, 350);
    }
    
    private void jButtonSolRecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSolRecActionPerformed
        Color cor = Color.GREEN;
        if (jCheckBox1.isSelected() &&
                !jCheckBox2.isSelected() &&
                !jCheckBox3.isSelected() &&
                !jCheckBox4.isSelected()) {
            selection[0] = true;
            selection[1] = false;
            selection[2] = false;
            selection[3] = false;
        } else if (!jCheckBox1.isSelected() &&
                jCheckBox2.isSelected() &&
                !jCheckBox3.isSelected() &&
                !jCheckBox4.isSelected()) {
            selection[0] = false;
            selection[1] = true;
            selection[2] = false;
            selection[3] = false;
        } else if (!jCheckBox1.isSelected() &&
                !jCheckBox2.isSelected() &&
                jCheckBox3.isSelected() &&
                !jCheckBox4.isSelected()) {
            selection[0] = false;
            selection[1] = false;
            selection[2] = true;
            selection[3] = false;
        } else if (!jCheckBox1.isSelected() &&
                !jCheckBox2.isSelected() &&
                !jCheckBox3.isSelected() &&
                jCheckBox4.isSelected()) {
            selection[0] = false;
            selection[1] = false;
            selection[2] = false;
            selection[3] = true;
        } else if (jCheckBox1.isSelected() &&
                jCheckBox2.isSelected() &&
                !jCheckBox3.isSelected() &&
                !jCheckBox4.isSelected()) {
            selection[0] = true;
            selection[1] = true;
            selection[2] = false;
            selection[3] = false;
        } else if (!jCheckBox1.isSelected() &&
                jCheckBox2.isSelected() &&
                jCheckBox3.isSelected() &&
                !jCheckBox4.isSelected()) {
            selection[0] = false;
            selection[1] = true;
            selection[2] = true;
            selection[3] = false;
        } else if (!jCheckBox1.isSelected() &&
                !jCheckBox2.isSelected() &&
                jCheckBox3.isSelected() &&
                jCheckBox4.isSelected()) {
            selection[0] = false;
            selection[1] = false;
            selection[2] = true;
            selection[3] = true;
        } else if (jCheckBox1.isSelected() &&
                !jCheckBox2.isSelected() &&
                !jCheckBox3.isSelected() &&
                jCheckBox4.isSelected()) {
            selection[0] = true;
            selection[1] = false;
            selection[2] = false;
            selection[3] = true;
        } else if (jCheckBox1.isSelected() &&
                jCheckBox2.isSelected() &&
                jCheckBox3.isSelected() &&
                !jCheckBox4.isSelected()) {
            selection[0] = true;
            selection[1] = true;
            selection[2] = true;
            selection[3] = false;
        } else if (!jCheckBox1.isSelected() &&
                jCheckBox2.isSelected() &&
                jCheckBox3.isSelected() &&
                jCheckBox4.isSelected()) {
            selection[0] = false;
            selection[1] = true;
            selection[2] = true;
            selection[3] = true;
        } else if (jCheckBox1.isSelected() &&
                !jCheckBox2.isSelected() &&
                jCheckBox3.isSelected() &&
                jCheckBox4.isSelected()) {
            selection[0] = true;
            selection[1] = false;
            selection[2] = true;
            selection[3] = true;
        } else if (jCheckBox1.isSelected() &&
                jCheckBox2.isSelected() &&
                !jCheckBox3.isSelected() &&
                jCheckBox4.isSelected()) {
            selection[0] = true;
            selection[1] = true;
            selection[2] = false;
            selection[3] = true;
        } else {
            JOptionPane.showMessageDialog(null, "Rota não permitida", null, 1);
            return;
        }
        if (selection[0]) {
            ((Painel) jPanel1).desenharQuadrado(150, 150, 50, 50, cor);
        }
        if (selection[1]) {
            ((Painel) jPanel1).desenharQuadrado(200, 150, 50, 50, cor);
        }
        if (selection[2]) {
            ((Painel) jPanel1).desenharQuadrado(200, 100, 50, 50, cor);
        }
        if (selection[3]) {
            ((Painel) jPanel1).desenharQuadrado(150, 100, 50, 50, cor);
        }
        jButtonSolRec.setEnabled(false);
    }//GEN-LAST:event_jButtonSolRecActionPerformed

    private void jButtonDefPistaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDefPistaActionPerformed
        Color cor = new Color(51, 51, 51);
        ((Painel) jPanel1).desenharQuadrado(120, 165, 15, 15, cor);
        ((Painel) jPanel1).desenharQuadrado(215, 220, 15, 15, cor);
        ((Painel) jPanel1).desenharQuadrado(270, 120, 15, 15, cor);
        ((Painel) jPanel1).desenharQuadrado(170, 70, 15, 15, cor);

        if (jComboBox2.getSelectedItem().toString().equals("Pista 1")) {
            ((Painel) jPanel1).desenharQuadrado(120, 165, 15, 15, Color.red);
        } else if (jComboBox2.getSelectedItem().toString().equals("Pista 2")) {
            ((Painel) jPanel1).desenharQuadrado(215, 220, 15, 15, Color.red);
        } else if (jComboBox2.getSelectedItem().toString().equals("Pista 3")) {
            ((Painel) jPanel1).desenharQuadrado(270, 120, 15, 15, Color.red);
        } else if (jComboBox2.getSelectedItem().toString().equals("Pista 4")) {
            ((Painel) jPanel1).desenharQuadrado(170, 70, 15, 15, Color.red);
        }
    }//GEN-LAST:event_jButtonDefPistaActionPerformed

    private void jButtonLibRecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLibRecActionPerformed
        ((Painel) jPanel1).repaint();
        jButtonSolRec.setEnabled(true);
    }//GEN-LAST:event_jButtonLibRecActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonDefPista;
    private javax.swing.JButton jButtonLibRec;
    private javax.swing.JButton jButtonSolRec;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
